package com.meixiang.activity.account.manager;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.main.MXApplication;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.CheckEditTextEmptyButton;
import com.meixiang.view.ClearEditText;
import com.meixiang.view.TitleView;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 验证支付密码手机短信
 *
 * @author zys
 */
public class VerifyPhoneNumberPayOffActivity extends BaseActivity {

    @Bind(R.id.title)
    TitleView title;
    @Bind(R.id.cet_input_verify_phone_number)
    ClearEditText cetInputVerifyPhoneNumber;
    @Bind(R.id.tv_acquire_verification)
    TextView tvAcquireVerification;
    @Bind(R.id.tv_confirm)
    CheckEditTextEmptyButton tvConfirm;
    private int curTime;
    private static final int INTERVAL = 1;
    private Activity mAcitity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_verify_phone_numeber_payoff);
        mAcitity=VerifyPhoneNumberPayOffActivity.this;
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initTitle() {
        title.setTitle(R.string.verfy_phone_name);
        title.setLeftImageButton(R.mipmap.back);
        title.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }


    @Override
    protected void initView() {
        tvAcquireVerification.setOnClickListener(goVerificatiop);//邀请码按钮
        tvConfirm.setEditText(cetInputVerifyPhoneNumber);
        tvConfirm.setOnClickListener(this);
    }



    @Override
    protected void setData() {
        MXApplication.mApp.AddActivity(mAcitity);
    }

    @Override
    public void onClick(View v) {
        if (v.equals(tvConfirm)) {
            VerifyCode();
        }

    }

    private View.OnClickListener goVerificatiop = new View.OnClickListener() {//邀请码按钮


        @Override
        public void onClick(View v) {
            HttpUtils.post(Config.SENDSMS_URL,null,new HttpCallBack(VerifyPhoneNumberPayOffActivity.this,"正在发送验证码...") {
                @Override
                public void onSucceed(JSONObject result, String code, String message) {
                    if (code.equals("0")){

                        setSendCode(true);
                        //  AbToastUtil.showToast(LoginVerifyPhoneNumberActivity.this, message);
                    }

                }

                @Override
                public void onError(String code, String message) {
                    AbToastUtil.showToast(VerifyPhoneNumberPayOffActivity.this, message);
                }
            });

        }
    };

    class TextChange implements TextWatcher {

        // 之后
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }

        // 之前
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {

        }


        // 改变 login按钮的背景
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            boolean Sign1 = false;
            if (cetInputVerifyPhoneNumber.getText().length() > 0) {
                Sign1 = true;
                tvAcquireVerification.setEnabled(true);
            } else {
                Sign1 = false;
                tvAcquireVerification.setEnabled(false);
            }

            if (Sign1 == true) {

                tvConfirm.setBackgroundResource(R.drawable.btn_redclick_selector_one);
                tvConfirm.setEnabled(true);
                tvConfirm.setOnClickListener(goVerificatiop);
            } else {
                tvConfirm.setBackgroundResource(R.drawable.btn_gray_background);
                tvConfirm.setEnabled(false);
            }
        }

    }

    // 从新发送
    private void setSendCode(boolean send) {
        curTime = 60;
        if (send == true) {
            mHandler.sendEmptyMessage(INTERVAL);
            tvAcquireVerification.setClickable(false);
        } else {
            tvAcquireVerification.setText(R.string.verfy_agin_send);
            tvAcquireVerification.setTextColor(ContextCompat.getColor(context, R.color.light_blue));
            tvAcquireVerification.setClickable(true);
        }
    }

    // 线程管理时间
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case INTERVAL:
                    if (curTime > 0) {
                        tvAcquireVerification.setText("" + curTime + "s");
                        tvAcquireVerification.setTextColor(ContextCompat.getColor(context, R.color.gray_cf));
                        mHandler.sendEmptyMessageDelayed(INTERVAL, 1000);
                        curTime--;
                    } else {
                        setSendCode(false);
                    }
                    break;
                default:
                    break;
            }
        }

        ;
    };
    private  void VerifyCode(){
        HttpParams params = new HttpParams();
        params.put("code",cetInputVerifyPhoneNumber.getText().toString());
        OkHttpUtils.post(Config.VERIFYCODE_URL).tag(this).params(params).execute(new HttpCallBack(VerifyPhoneNumberPayOffActivity.this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if (code.equals("0")){
                    Intent intent = new Intent(mAcitity, PayOffSetNewPasswordActivity.class);
                    intent.putExtra("ClassStatus","2");
                    startActivity(intent);
                    AbToastUtil.showToast(VerifyPhoneNumberPayOffActivity.this, message);
                }

            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(VerifyPhoneNumberPayOffActivity.this, message);
            }
        });

    }
}
